//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using System.Threading;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class CategoryDetailsControl : BaseNopAdministrationUserControl
    {
        protected override void OnInit(EventArgs e) {
            SaveButton.Click+=new EventHandler(SaveButton_Click);
            DeleteButton.Click+=new EventHandler(DeleteButton_Click);

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e) {
            var category = this.CategoryService.GetCategoryById(this.CategoryId);
            if (category != null) {
                PreviewButton.OnClientClick = string.Format("javascript:OpenWindow('{0}', 800, 600, true); return false;", SEOHelper.GetCategoryUrl(category.CategoryId));
                if (!Page.IsPostBack) {
                    lblTitle.Text = Server.HtmlEncode(category.Name);
                }
            }

            base.OnLoad(e);
        }

        protected Category Save() {
            //Category category = null;

            Category category = ctrlCategoryInfo.SaveInfo();
            ctrlCategorySEO.SaveInfo();
            ctrlCategoryProduct.SaveInfo();
            ctrlCategoryDiscount.SaveInfo();
            ctrlCategoryACL.SaveInfo();

            this.CustomerActivityService.InsertActivity("EditCategory", GetLocaleResourceString("ActivityLog.EditCategory"), category.Name);

            Thread.Sleep(1000);
            return category;
        }

        protected void SaveButton_Click(object sender, EventArgs e) {
            if (Page.IsValid) {
                try {
                    Category category = Save();
                    //Response.Redirect("Categories.aspx");
                } catch (Exception exc) {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e) {
            try {
                var category = this.CategoryService.GetCategoryById(this.CategoryId);
                if (category != null) {
                    this.CategoryService.MarkCategoryAsDeleted(category.CategoryId);

                    this.CustomerActivityService.InsertActivity("DeleteCategory", GetLocaleResourceString("ActivityLog.DeleteCategory"), category.Name);
                }
                Response.Redirect("Categories.aspx");
            } catch (Exception exc) {
                ProcessException(exc);
            }
        }

        public int CategoryId {
            get {
                return CommonHelper.QueryStringInt("CategoryId");
            }
        }

        protected string TabId {
            get {
                return CommonHelper.QueryString("TabId");
            }
        }
    }
}